/**********************************************************************************************************************
 *
 * Project:    mGameEngine
 * File:       soundsource.h
 * Author:     karooolek
 * Created on: 2009-03-24
 *
 **********************************************************************************************************************/

#ifndef SOUNDSOURCE_H_
#define SOUNDSOURCE_H_

#include "sound.h"
#include <AL/al.h>

namespace mGameEngine
{
namespace Audio
{

/**
 * Sound source base class.
 * Each sound source has sound attached to it.
 * Provides interface for playback control.
 */
class SoundSource
{
protected:
    /**
     * Source id.
     */
    uint _source;

    /**
     * Sound attached to sound source
     */
    Sound *_sound;

    /**
     * Sound playing.
     */
    bool _play;

    /**
     * Gain.
     */
    float _gain;

    /**
     * Sound playing loops number.
     */
    uint _loop;

public:
    /**
     * Create sound source.
     */
    SoundSource();

    /**
     * Create sound source with specified sound and playing loops number.
     * @param sound sound source to attach.
     * @param loop playing loops number. -1 means infinite loop.
     */
    SoundSource(Sound *sound, uint loop = -1);

    /**
     * Destroy sound source.
     */
    virtual ~SoundSource();

    /**
     * Get sound attached to sound source.
     * @return sound attached to sound source
     */
    Sound *getSound() const
    {
        return _sound;
    }

    /**
     * Attach sound to sound source.
     * @param sound sound to attach
     */
    void setSound(Sound *sound)
    {
        _sound = sound;
    }

    /**
     * Get gain.
     * @return gain.
     */
    float getGain() const
    {
        return _gain;
    }

    /**
     * Set gain.
     * @param gain gain.
     */
    void setGain(float gain)
    {
        _gain = gain;
    }

    /**
     * Get playing loops number.
     * @return playing loops number. -1 means infinite loop.
     */
    uint8 getLoop() const
    {
        return _loop;
    }

    /**
     * Set playing loops number
     * @param loop playing loops number. -1 means infinite loop.
     */
    void setLoop(uint loop)
    {
        _loop = loop;
    }

    /**
     * Check if sound is beeing played.
     * @return true if sound is beeing played, false otherwise
     */
    bool isPlaying() const
    {
        return _play;
    }

    /**
     * Play sound.
     */
    virtual void play();

    /**
     * Pause sound.
     */
    virtual void pause();

    /**
     * Stop sound.
     * Pause and go to sound beginning.
     */
    virtual void stop();

    /**
     * Set playing state.
     * @param play true to play, false to stop.
     */
    void setPlaying(bool play)
    {
        if(play)
        {
            this->play();
        }
        else
        {
            stop();
        }
    }

    /**
     * Toggle playing state.
     */
    void togglePlaying()
    {
        setPlaying(!_play);
    }

    /**
     * Get playing offset (position).
     * @return playing offset (position) in seconds
     */
    virtual float getOffset() const;

    /**
     * Set playing offset (position).
     * @param offset playing offset (position) in seconds
     */
    virtual void setOffset(float offset);

    /**
     * Set sound source.
     */
    virtual void set() const = 0;
};

}
}

#endif // SOUNDSOURCE_H_
